package web;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import vo.User;
import business.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

import constant.Constant;

@SuppressWarnings("unchecked")
public class ImageAction implements Preparable {

	private User curUser;
	private UserService userService;

	private String topicId;

	private void wrapImage(String file) {
		HttpServletResponse response = null;
		ServletOutputStream out = null;
		InputStream is = null;
		byte[] bytes = null;
		try {
			is = new BufferedInputStream(new FileInputStream(file)); // mid.jpg图片要与HelloJava.java同在一目录下
			response = ServletActionContext.getResponse();
			response.setContentType("multipart/form-data");
			out = response.getOutputStream();
			bytes = new byte[1024];
			while (-1 != is.read(bytes)) {
				out.write(bytes);
			}
			// 强制刷新输出流
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			bytes = null;
		}

	}

	public String imageSources() throws Exception {
		System.out.println("imageSources:" + Constant.root + curUser.getId() + "/imageSources.jpg");
		wrapImage(Constant.root + curUser.getId() + "/imageSources.jpg");
		return null;
	}

	public String imageTrends() throws Exception {
		System.out.println("imageTrends:" + Constant.root + curUser.getId() + "/imageTrends.jpg");
		wrapImage(Constant.root + curUser.getId() + "/imageTrends.jpg");
		return null;
	}

	public String imageTrendsTopic() throws Exception {
		System.out.println("imageTrendsTopic:" + Constant.root + curUser.getId() + "/imageTrends_topic.jpg");
		wrapImage(Constant.root + curUser.getId() + "/imageTrends_topic.jpg");
		return null;
	}

	// 按照topic显示趋势
	public String imageTrendsByTopicId() throws Exception {
		System.out.println("imageTrendsByTopicId:" + topicId);
		System.out.println(Constant.root + "topic" + "/" + topicId + "_imageTrends.jpg");
		wrapImage(Constant.root + "topic" + "/" + topicId + "_imageTrends.jpg");
		return null;
	}

	// 按照topic显示媒体在组成
	public String imageSourcesByTopicId() throws Exception {
		System.out.println("imageSourcesByTopicId:" + topicId);
		System.out.println(Constant.root + "topic" + "/" + topicId + "_imageSources.jpg");
		wrapImage(Constant.root + "topic" + "/" + topicId + "_imageSources.jpg");
		return null;
	}
	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser:" + curUser.getLoginName());
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public String getTopicId() {
		return topicId;
	}

	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}

}